var api = require('../../config/api.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        customerCode: '',
        viewIndex: 1,
        feedBackState: 1,
        feedbackGlance: 0,
        footer: {},
        infoList: [],
        hikFeedbackStatisticsInfo: {
            'handledNum': 0,
            'observeNum': 0,
            'pendingNum': 0,
            'rejectNum': 0,
        },
        mPageNum: 1,
        // 显示当前页面时，需要调用接口，获取unreadCount的数量
        unreadNum: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getfooter()
        this.doHttpGetHikFeedbackStatistics()
        this.doHttpGetHikFeedbackStatisticsList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        this.popup = this.selectComponent("#popup");
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        var that = this
        that.setData({
            mPageNum: 1
        })
        // 更新底部导航栏未读消息数量
        that.getfooter()
        that.doHttpGetHikFeedbackStatistics()
        that.doHttpGetHikFeedbackStatisticsList()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        var that = this
        let num = that.data.mPageNum
        that.setData({
            mPageNum: num += 1
        });
        that.doHttpGetHikFeedbackStatisticsList()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    /**
     * 初始化底部Tab数据
     */

    getfooter: function () {
        var that = this
        wx.getStorage({
            key: 'psCustomerCode',
            success: (RES) => {
                that.setData({
                    customerCode: RES.data
                });
                console.log('customerCode为', RES.data)
                wx.request({
                    url: api.findUnread,
                    method: 'POST',
                    data: {
                        psCustomerCode: that.data.customerCode,
                    },
                    success: function (res) {
                        if (res.data.code == 200) {
                            that.setData({
                                unreadNum: res.data.data.unreadNum,
                            }),
                            console.log("未读消息数量:", that.data.unreadNum)
                            // var t = this;
                            var footerList = {
                                footerTab: 1,
                                footerlist: [{
                                    diy_name: "设备概览",
                                    color: '#221915',
                                    img: "",
                                    selimg: "",
                                    link: "/pages/index/index"
                                }, {
                                    diy_name: "实时监测",
                                    color: '#221915',
                                    img: "",
                                    selimg: "",
                                    link: "/pages/indexMonitor/indexMonitor",
                                    unreadNum: that.data.unreadNum
                                }, {
                                    diy_name: "",
                                    color: '#221915',
                                    img: "https://www.cqwuyang.cn/images/Recognition.png",
                                    selimg: "https://www.cqwuyang.cn/images/Recognition.png",
                                    link: ""
                                }, {
                                    diy_name: "上报总览",
                                    color: '#221915',
                                    img: "",
                                    selimg: "",
                                    link: "/pages/indexSummary/indexSummary"
                                }, {
                                    diy_name: "个人中心",
                                    color: '#221915',
                                    img: "",
                                    selimg: "",
                                    link: "/pages/indexMy/indexMy"
                                }]
                            }
                            that.setData({
                                footer: footerList
                            })
                        }

                    }
                })
            },
        });



    },

    /**
     * 获取实时监测海康上报数据统计
     */
    doHttpGetHikFeedbackStatistics: function () {
        var that = this
        wx.getStorage({
            key: 'psCustomerCode',
            success: (RES) => {
                that.setData({
                    customerCode: RES.data
                });
                console.log('customerCode为', RES.data)
                wx.request({
                    url: api.getHikFeedbackStatistics,
                    method: 'POST',
                    data: {
                        psCustomerCode: that.data.customerCode,
                        feedbackType: that.data.viewIndex,
                    },
                    success: function (res) {
                        if (res.data.code == 200) {
                            that.setData({
                                hikFeedbackStatisticsInfo: res.data.data,
                            });
                        }
                    }
                })
            },
        });
    },

    /**
     * 获取实时监测海康上报数据列表
     */
    doHttpGetHikFeedbackStatisticsList: function () {
        var that = this
        wx.getStorage({
            key: 'psCustomerCode',
            success: (RES) => {
                that.setData({
                    customerCode: RES.data
                });
                console.log('customerCode为', RES.data)

                wx.request({
                    url: api.feedbackFindAllByCondition,
                    method: 'POST',
                    data: {
                        psCustomerCode: that.data.customerCode,
                        feedbackType: that.data.viewIndex,
                        feedbackState: that.data.feedBackState,
                        feedbackClassification: 1,
                        pageNum: that.data.mPageNum,
                        pageSize: 5
                    },
                    success: function (res) {
                        if (that.data.mPageNum != 1) {
                            that.setData({
                                infoList: that.data.infoList.concat(res.data.data.rows)
                            });
                        } else {
                            wx.stopPullDownRefresh()
                            that.setData({
                                infoList: res.data.data.rows
                            });
                        }
                        console.log("infoList内容", that.data.infoList)
                    }
                })
            },
        });

    },

    changeRange: function () {
        this.popup.changeRange();
    },

    /**
     * 点击报警信息
     */
    switchLeft: function () {
        this.setData({
            viewIndex: 1,
            feedBackState: 1
        });
        this.doHttpGetHikFeedbackStatistics()
        this.doHttpGetHikFeedbackStatisticsList()
        wx.startPullDownRefresh()
    },

    /**
     * 点击故障信息
     */
    switchRight: function () {
        this.setData({
            viewIndex: 2,
            feedBackState: 1
        });
        this.doHttpGetHikFeedbackStatistics()
        this.doHttpGetHikFeedbackStatisticsList()
        wx.startPullDownRefresh()
    },

    /**
     * 点击待处理
     */
    onCheckOne: function () {
        var that = this
        that.setData({
            feedBackState: 1
        });
        that.setData({
            mPageNum: 1
        });
        that.doHttpGetHikFeedbackStatisticsList()
    },

    /**
     * 点击已驳回
     */
    onCheckTwo: function () {
        var that = this
        that.setData({
            feedBackState: 2
        });
        that.setData({
            mPageNum: 1
        });
        that.doHttpGetHikFeedbackStatisticsList()
    },

    /**
     * 点击留观中
     */
    onCheckThree: function () {
        var that = this
        that.setData({
            feedBackState: 3
        });
        that.setData({
            mPageNum: 1
        });
        that.doHttpGetHikFeedbackStatisticsList()
    },

    /**
     * 点击已处置
     */
    onCheckFour: function () {
        var that = this
        that.setData({
            feedBackState: 4
        });
        that.setData({
            mPageNum: 1
        });
        that.doHttpGetHikFeedbackStatisticsList()
    },

    todeviceDetails: function (event) {
        var that = this
        // 获取点击的 item
        const item = event.currentTarget.dataset.idtype;
        // 获取 feedbackCode
        const feedbackCode = item.feedbackCode;
        //  用户点击之后，调用修改已读状态接口
        wx.request({
            url: api.upAppointUnread,
            method: 'POST',
            data: {
                feedbackCode: feedbackCode,
                psCustomerCode: that.data.customerCode,
            },
            success: function (res) {
                if (res.data.code == 200) {
                    console.log("修改当前状态为已读")
                    // 执行页面刷新
                    wx.startPullDownRefresh();
                }
            }
        })
    }

})